#!/bin/bash
#BASE_BIN_DIR=`dirname $0`
#. $BASE_BIN_DIR/functions.sh
. `pwd`/functions.sh

#========begin enviroment var define
export JAVA_HOME=/usr/sumpu/java
export JETTY_HOME=/usr/sumpu/jetty
export JETTY_DEPLOY_HOME=/home/yulong/work/webapps
export APP_HOME=/home/yulong/work/test/guoguojie/guoguojie-deploy
export OUTPUT_HOME=$APP_HOME/output
export APP_PORT=8080
export DEBUG_PORT=8888
export PRODUCTION_MODE=dev
export JMX_PORT=9999

export LANG=zh_CN.utf8
export CHECK_STARTUP_URL="http://127.0.0.1:$APP_PORT/test.ajax"
export STARTUP_SUCCESS_MSG="start jetty successfully"
export JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
export JAVA_DEBUG_OPT=" -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n "

if [ ! -e $JAVA_HOME ]; then
	failed "********************************************************************"
	failed "**Error: $JAVA_HOME not exist"
	failed "********************************************************************"    
	exit 1
fi

if [ ! -e $JETTY_HOME ]; then
	failed "********************************************************************"
	failed "**Error: $JETTY_HOME not exist."
	failed "********************************************************************"
	exit 1
fi
#========end enviroment var define

if [ $PRODUCTION_MODE = "run" ]; then
	#after the environment of online is all 64-bit,the below if and else judgement can be delete
	str=`file $JAVA_HOME/bin/java | grep 64-bit`
	if [ -n "$str" ]; then
        	let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'`
        	if [ $memTotal -gt 2500 ];then            
			JAVA_MEM_OPTS=" -server -Xmx1024m -Xms1024m -Xmn128m -XX:PermSize=96m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly "
		else 
            		JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn64m -XX:PermSize=64m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection  -XX:+UseCMSInitiatingOccupancyOnly "
	fi
	else
		JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=64m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
    	fi    
	JAVA_OPTS=" $JAVA_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
elif [ $PRODUCTION_MODE = "test" ]; then
	#JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:MaxPermSize=64m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
	JAVA_MEM_OPTS=" -server -Xmx1024m -Xms1024m -Xmn128m -XX:PermSize=96m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly "
	JAVA_OPTS=" $JAVA_MEM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
elif [ $PRODUCTION_MODE = "dev" ]; then
	#we shuold reduce resource usage on developing mode
	JAVA_MEM_OPTS=" -server -Xms64m -Xmx1024m -XX:MaxPermSize=128m "
	JAVA_OPTS=" $JAVA_MEM_OPTS $JAVA_DEBUG_OPT "
	JAVA_OPTS=" $JAVA_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
else
	failed "********************************************************************"
	failed "**Error: \$PRODUCTION_MODE should be only: run, test, dev"
	failed "********************************************************************"    
	exit 1
fi

export JETTY_SERVER_DIR=" -Djetty.home=$JETTY_HOME "
JAVA_OPTS=" $JAVA_OPTS $JETTY_SERVER_DIR"

deploy_war(){
	if [ $PRODUCTION_MODE = "run" ]; then
    		rm -rf  "$JETTY_DEPLOY_HOME/web.war"
  		if [ -f "$APP_HOME/guoguojie-web.war" ] ; then
			ln -s  "$APP_HOME/guoguojie-web.war"  "$JETTY_DEPLOY_HOME/web.war"
		elif [ -f "$APP_HOME/target/deploy/guoguojie-web.war" ] ; then
			ln -s   "$APP_HOME/target/deploy/guoguojie-web.war"  "$JETTY_DEPLOY_HOME/web.war"
		elif [ -f "$OUTPUT_HOME/web.war" ] ; then
			ln -s   "$OUTPUT_HOME/web.war"  "$JETTY_DEPLOY_HOME/web.war"
		else
           		failed "********************************************************************"
    			failed "**Error: guoguojie-web.war not exist."
    			failed "********************************************************************"    
    		exit 1
	fi  	    
	else
		if [ -f  "$JETTY_DEPLOY_HOME/web.war" ] ; then
        		rm -rf  "$JETTY_DEPLOY_HOME/web.war"
            		echo "Remove link file:  $JETTY_DEPLOY_HOME/web.war"
        	fi
        	cp   "$APP_HOME/target/deploy/guoguojie-web.war"  "$JETTY_DEPLOY_HOME/web.war"
    	fi
}

copy_jetty_conf(){
	cp  "$APP_HOME/conf/jetty/jetty-webapps.xml"  "$JETTY_HOME/etc/jetty-webapps.xml"
	cp  "$APP_HOME/conf/jetty/guoguojie.xml"  "$JETTY_HOME/contexts/guoguojie.xml"
	if [ ! -d "$JETTY_HOME/contexts/guoguojie.d" ] ; then
		mkdir "$JETTY_HOME/contexts/guoguojie.d"
	fi
	cp -r "$APP_HOME/conf/jetty/override-web.xml"  "$JETTY_HOME/contexts/guoguojie.d/override-web.xml" 
}

deploy(){
    copy_jetty_conf
    deploy_war
}

